package com.platinadesign.axa.loader
{
	import com.platinadesign.axa.event.ConfigureEvent;
	import com.platinadesign.axa.helper.ConfigureDataPaser;
	import com.platinadesign.axa.vo.ConfigueData;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;

	public class ConfigureLoader extends EventDispatcher
	{
		private var loader : URLLoader;

		public function loadConfigure( url : String ) : void
		{
			loader = new URLLoader();
			const request : URLRequest = new URLRequest( url );
			loader.addEventListener( Event.COMPLETE, completeHandler );
			loader.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler );

			loader.load( request );
		}

		protected function ioErrorHandler( event : IOErrorEvent ) : void
		{
			this.dispatchEvent( new ConfigureEvent( ConfigureEvent.CONFIGURE_LOAD_ERROR ));
		}

		protected function completeHandler( event : Event ) : void
		{
			const paser : ConfigureDataPaser = new ConfigureDataPaser();
			const configure : ConfigueData = paser.pase( XML( loader.data ));
			this.dispatchEvent( new ConfigureEvent( ConfigureEvent.CONFIGURE_LOADED, configure ));
		}
	}
}